package com.zeus.gen.base.controller;

import com.zeus.gen.base.dto.Query4Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.zeus.gen.base.model.BaseModel;
import com.zeus.gen.base.service.BaseService;

import java.io.Serializable;
import java.util.List;

/**
 * 功能说明：BaseController 基类，处理异常，公共方法
 *
 * @param <T>        实体对象
 * @param <QueryDto> 查询对象
 * @author liuwei
 */
public abstract class BaseController<T extends BaseModel, QueryDto> {
    protected static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);

    /**
     * 文件前缀
     *
     * @return
     */
    protected abstract String getPrefix();

    /**
     * 获取对应service
     *
     * @return
     */
    protected abstract BaseService<T> getBaseService();

    protected final String INDEX = getPrefix() + "/index";
    protected final String LIST = getPrefix() + "/list";
    protected final String EDIT = getPrefix() + "/edit";
    protected final String VIEW = getPrefix() + "/view";

    /**
     * 通用列表查询条件方法
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public String index(@ModelAttribute QueryDto params, @ModelAttribute Query4Page<QueryDto> page, Model model) {
        this.list(params, page, model);
        return INDEX;
    }

    /**
     * 通用列表查询方法
     *
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(@ModelAttribute QueryDto params, @ModelAttribute Query4Page<QueryDto> page, Model model) {
        page.putAll(params);
        List list = this.getBaseService().findList(page);
        model.addAttribute("list", list);
        model.addAttribute("page", page);
        return LIST;
    }

    @RequestMapping(value = "/view/{id}")
    public String view(Model model, @PathVariable("id") Serializable id) {
        T obj = this.getBaseService().findObjById(id);
        model.addAttribute("model", obj);
        return VIEW;
    }

    /**
     * 通用跳转到新增页面
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        return EDIT;
    }

    /**
     * 通用跳转到编辑页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit/{id}")
    public String edit(Model model, @PathVariable("id") Serializable id) {
        this.view(model, id);
        return EDIT;
    }

    /**
     * 保存或修改对象
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    public String saveOrUpdate(Model model, T t, @ModelAttribute QueryDto params, @ModelAttribute Query4Page<QueryDto> page) {
        this.getBaseService().saveOrUpdate(t);
        this.list(params, page, model);
        return INDEX;
    }

    /**
     * 删除(默认无实现)
     *
     * @return
     */
    @RequestMapping(value = "/delete/{id}")
    public String delete(Model model, @PathVariable("id") Serializable id, @ModelAttribute QueryDto params, @ModelAttribute Query4Page<QueryDto> page) {
//        this.getBaseService().delete(id);
        return this.list(params, page, model);
    }
}
